package com.feiyu.universal.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.feiyu.universal.entity.Dict;
import com.feiyu.universal.mapper.DictMapper;
import com.feiyu.universal.service.DictService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author tang ming
 * @since 2021-11-30
 */
@Service
public class DictServiceImpl extends ServiceImpl<DictMapper, Dict> implements DictService {

    @Override
    public List<Dict> listAllLeaf(int id, String code) {
        QueryWrapper<Dict> wrapper = new QueryWrapper<>();
        if (code != null && code.length() > 0)
            wrapper.eq("code", code);
        if (id != 0)
            wrapper.eq("parentId", id);
        List<Dict> list = list(wrapper);
        ArrayList<Dict> result = new ArrayList<>();
        for (Dict item : list) {
            //如果是叶子节点直接加
            if (item.getLeaf())
                result.add(item);
                //加其叶子
            else if (id!=0)result.addAll(listAllLeaf(item.getId(), code));
        }
        return result;
    }

}
